package api

import (
	"gitee.com/cyanbleeze/go-image-management/internal/service"
	"gitee.com/cyanbleeze/go-image-management/pkg/app"
	"gitee.com/cyanbleeze/go-image-management/pkg/errcode"
	"github.com/gin-gonic/gin"
)

type Upload struct{}

func NewUpload() Upload {
	return Upload{}
}

func (u Upload) UploadFile(ctx *gin.Context) {
	res := app.NewResponse(ctx)
	file, err := ctx.FormFile("file")
	if err != nil {
		res.ToErrorResponse(errcode.InvalidParams.WithDetails(err.Error()))
		return
	}
	svc := service.New(ctx)
	accessUrl, err2 := svc.UploadFile(file)
	if err2 != nil {
		res.ToErrorResponse(errcode.ErrorUploadFileFail.WithDetails(err2.Error()))
		return
	}
	res.ToResponse(gin.H{"access_url": accessUrl})
}
